package cn.uexpo.lhz.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.uexpo.lhz.biz.ExpoBiz;
import cn.uexpo.lhz.biz.HallBiz;
import cn.uexpo.lhz.common.oldBase.BaseController;
import cn.uexpo.lhz.core.http.HttpResult;
import cn.uexpo.lhz.domain.Expo;
import cn.uexpo.lhz.domain.Hall;
import cn.uexpo.lhz.domain.HallExample;


/**
 * 
 * @ClassName:  ExpoController   
 * @Description:TODO(配置展馆)   
 * @author: 李冬建
 * @date:   2020年6月4日 上午10:26:58      
 * @Copyright:  1.0.0
 */
@RestController
@RequestMapping("/admin/configHall")
public class ExpoHallConfigController extends BaseController<Expo>{
	
	@Autowired
	private ExpoBiz expoBiz;
	@Autowired
	private HallBiz hallBiz;

	
	/**
	 * 
	 * @Title: list   
	 * @Description: TODO(思路：展馆启用禁用只对配置有效，对小程序端无效。启用的展馆才有可能能被展会配置。
	 * 							小程序端的展示只根据展会显示与否控制，后台让展会显示了，小程序端才能展示，具体的展示信息在配置信息里面。)
	 * @param: @return      
	 * @return: HttpResult      
	 * @throws
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public HttpResult  list() {
		HallExample example=new HallExample();
		HallExample.Criteria criteria=example.createCriteria();
		criteria.andUsableEqualTo(1);//启用的展馆才被配置，禁用的展馆不能被配置
		List<Hall> list = hallBiz.selectByExample(null);
		return HttpResult.ok(list);
	}

	
	/**
	 * 
	 * @Title: subConfig   
	 * @Description: TODO(提交配置方法：保存在event表的config字段，格式为 展会id##展馆id##日期年月日信息##日期时分秒信息，
	 * 								    其中展馆id可以是多个，中间以英文逗号隔开，
	 *  							    其中日期年月日可以是多个，中间以英文逗号隔开，
	 *   							    其中日期时分秒可以是多个，中间以@隔开，数量和年月日一样；同一天可以有多个时间点，之间以英文逗号隔开；开始结束时间点之间以-隔开
	 *   				  比如：13##26,27##2019-12-11,2019-12-12##02:00:00-22:00:00@02:00:00-05:00:00,02:00:00-20:00:00
	 *   				 展会id是13，展馆id是26，27，日期是{2019-12-11 02:00:00-22:00:00} 和 {2019-12-12 02:00:00-05:00:00,2019-12-12 02:00:00-20:00:00})   
	 * @param: @param pageRequest
	 * @param: @return      
	 * @return: HttpResult      
	 * @throws
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "/subConfig", method = { RequestMethod.GET, RequestMethod.POST })
	public HttpResult  subConfig(@RequestBody cn.uexpo.lhz.common.oldUtils.PageRequest pageRequest) {
		Object hallIds = pageRequest.getParamValue("hallIds");//展馆ids
		Object dates = pageRequest.getParamValue("dates");//日期信息
		Object enventId = pageRequest.getParamValue("enventId");//展会id
		String msg="";
//		重新封装，存储到表event中的config字段
		int expoId=0; //展会id
		int[] hallArray=null;//展馆ids
		JSONArray date=null;//日期信息
		String hallStr="";
		String dayStr="";
		if (StringUtils.isNotBlank((String)enventId)) {
			 expoId=Integer.parseInt((String)enventId);
		}else {
			msg="请选择展会！";
    		return HttpResult.error(msg);
		}
		if (StringUtils.isNotBlank((String)hallIds)) {
			 hallStr=getHallStr((String)hallIds);
		}else {
			msg="请选择展馆！";
    		return HttpResult.error(msg);
		}
		if (StringUtils.isNotBlank((String)dates)) {
			date = JSONArray.parseArray((String)dates);
			dayStr=getDateStr(date);
		}else {
			msg="请选择日期！";
    		return HttpResult.error(msg);
		}
		
		String resultStr=expoId+"##"+hallStr+"##"+dayStr;//最终信息
		Expo record=expoBiz.selectByPrimaryKey(expoId);
		record.setConfig(resultStr);
		record.setExtstr1("1");//已配置:1，未配置:0
		int result=expoBiz.updateByPrimaryKeySelective(record);
		
		if(result==1) {
    		msg="配置成功！";
    		return HttpResult.ok(msg);
    	}else {
    		msg="配置失败！";
    		return HttpResult.error(msg);
		}
	
	}
	
	
		//获取展馆字符串
		private String getHallStr(String hallIds) {
			String resultStr="";
			resultStr=hallIds.substring(1,hallIds.length()-1);
			return resultStr;
		}
		//获取日期字符串
		private String getDateStr(JSONArray date) {
			String resultStr="";
			String dayStr="";
			String timeStr="";
			if (date!=null&&date.size()>0) {
				for (Object object : date) {
					JSONObject json=(JSONObject)object;
					String dayTime=(String)json.get("dayTime");
					dayStr+=dayTime+",";
				}
				for (Object object : date) {
					String temp="@";
					JSONObject json=(JSONObject)object;
					String time1Id_begin=(String)json.get("time1Id_begin");
					String time1Id_end=(String)json.get("time1Id_end");
					if (StringUtils.isNotBlank(time1Id_begin)&&StringUtils.isNotBlank(time1Id_end)) {
						temp+=time1Id_begin+"-"+time1Id_end+",";
					}
					String time2Id_begin=(String)json.get("time2Id_begin");
					String time2Id_end=(String)json.get("time2Id_end");
					if (StringUtils.isNotBlank(time2Id_begin)&&StringUtils.isNotBlank(time2Id_end)) {
						temp+=time2Id_begin+"-"+time2Id_end+",";
					}
					String time3Id_begin=(String)json.get("time3Id_begin");
					String time3Id_end=(String)json.get("time3Id_end");
					if (StringUtils.isNotBlank(time3Id_begin)&&StringUtils.isNotBlank(time3Id_end)) {
						temp+=time3Id_begin+"-"+time3Id_end+",";
					}
					String time4Id_begin=(String)json.get("time4Id_begin");
					String time4Id_end=(String)json.get("time4Id_end");
					if (StringUtils.isNotBlank(time4Id_begin)&&StringUtils.isNotBlank(time4Id_end)) {
						temp+=time4Id_begin+"-"+time4Id_end+",";
					}
					temp=temp.substring(0,temp.length()-1);
					timeStr+=temp;
				}
				
				log.info("-----------dayStr:"+dayStr);
				log.info("-----------timeStr:"+timeStr);
				dayStr=dayStr.substring(0,dayStr.length()-1);
				timeStr=timeStr.substring(1,timeStr.length());
				resultStr=dayStr+"##"+timeStr;
			}
			
			return resultStr;
		}

		
		
	
	/**
	 * 
	 * @Title: selectDetail   
	 * @Description: TODO(查看配置详情)   
	 * @param: @param pageRequest
	 * @param: @return      
	 * @return: HttpResult      
	 * @throws
	 */
	@RequestMapping(value = "/selectDetail", method = { RequestMethod.GET, RequestMethod.POST })
	public HttpResult  selectDetail(@RequestBody cn.uexpo.lhz.common.oldUtils.PageRequest pageRequest) {
		String msg="";
		String configStr="";
		String expoName="";
		String hallNames="";
		String dateInfo="";
		
		int eventId=0;
		Object expoId = pageRequest.getParamValue("expoId");//展会id
		if (StringUtils.isNotBlank((String)expoId)) {
			eventId=Integer.parseInt((String)expoId);
		}
		Map map =getConfig(eventId);
		Map map1 =new HashMap();
		String code=(String)map.get("code");
		if (code.equals("200")) {
			expoName=getExpoNameByid(eventId);
			hallNames=getHallNames(eventId);
			dateInfo=getDates(eventId);
			
			map1.put("expoInfo", expoName);
			map1.put("hallInfo", hallNames);
			map1.put("dateInfo", dateInfo);
//			configStr="展会名称："+expoName+"\n\n"+"展馆名称："+hallNames+"\n\n"+"日期信息："+dateInfo+"\n\n";
			return HttpResult.ok(map1);
		}else {
			msg="配置信息有误！";
    		return HttpResult.error(msg);
		}
		
	}
	

		@SuppressWarnings({ "unchecked","rawtypes" })
		private Map getConfig(int expoId) {
			Map map =new HashMap();
			map.put("code", "200");
			Expo record=expoBiz.selectByPrimaryKey(expoId);
			String configStr=record.getConfig();
			String arry[]=configStr.split("##");
			if (arry!=null&&arry.length>0) {
				try {
					String eventId=arry[0];//展会
					String hallIds=arry[1];//展馆
					String days=arry[2];//年月日
					String times=arry[3];//时分秒
					map.put("eventId", eventId);
					map.put("hallIds", hallIds);
					map.put("days", days);
					map.put("times", times);
				}catch (Exception e) {
					map.put("code", "500");
					map.put("msg", "配置信息有误！");
					return map;
				}
			}
			return map;
			
		}
		
		
		//展会名称
		private String getExpoNameByid(int expoId) {
			String resultStr="";
			Expo record=expoBiz.selectByPrimaryKey(expoId);
			if (record!=null) {
				resultStr=record.getName();
			}
			return resultStr;
		}
		
		//根据展会查看配置的展馆名字
		private String getHallNames(int expoId) {
			String resultStr="";
			String hallIds="";
			Map map =getConfig(expoId);
			if (map!=null) {
				String code=(String)map.get("code");
				if (code.equals("200")) {
					hallIds=(String)map.get("hallIds");
				}
			}
			resultStr=getHallNamesByids(hallIds);
			return resultStr;
		}
		//根据展会查看配置的日期（年月日时分秒）信息
		private String getDates(int expoId) {
			String resultStr="";
			String days="";
			String times="";
			Map map =getConfig(expoId);
			if (map!=null) {
				String code=(String)map.get("code");
				if (code.equals("200")) {
					days=(String)map.get("days");
					times=(String)map.get("times");
					resultStr=getDays(days, times);
				}
			}
			return resultStr;
		}
				
		
		
		
		//展馆id字符串
		private String getHallNamesByids(String hallIds) {
			String resultStr="";
			String hallId[]=hallIds.split(",");
			if (hallId!=null&&hallId.length>0) {
				for (String id : hallId) {
					Hall hallRecord=hallBiz.selectByPrimaryKey(Integer.parseInt(id));
					resultStr+=hallRecord.getName()+",";
				}
			}
			if (StringUtils.isNotBlank(resultStr)&&resultStr.length()>1) {
				resultStr=resultStr.substring(0,resultStr.length()-1);
			}
			return resultStr;
			
		}
		
		
		
		private String getDays(String days,String times) {
			String resultStr="";
			String day[]=days.split(",");
			String time[]=times.split("@");
			if (day!=null&&day.length>0) {
				for (int i = 0; i < day.length; i++) {
					resultStr+="{"+day[i]+"    "+time[i]+"}  ,";
				}
			}
			if (StringUtils.isNotBlank(resultStr)&&resultStr.length()>1) {
				resultStr=resultStr.substring(0,resultStr.length()-1);
			}
			return resultStr;
		}
		
		//根据展会查看配置的年月日信息
		private String getDays(String days) {
			String resultStr="";
			String day[]=days.split(",");
			if (day!=null&&day.length>0) {
				for (String str : day) {
					resultStr+=str+",";
				}
			}
			if (StringUtils.isNotBlank(resultStr)&&resultStr.length()>1) {
				resultStr=resultStr.substring(0,resultStr.length()-1);
			}
			return resultStr;
		}
		
		@RequestMapping(value = "/redoConfig", method = { RequestMethod.GET, RequestMethod.POST })
		public HttpResult  redoConfig(@RequestBody cn.uexpo.lhz.common.oldUtils.PageRequest pageRequest) {
			Object expoId = pageRequest.getParamValue("expoId");//展会id
			String msg="";
			int eventId=0;
			if (StringUtils.isNotBlank((String)expoId)) {
				eventId=Integer.parseInt((String)expoId);
			}
			Expo record=expoBiz.selectByPrimaryKey(eventId);
			record.setConfig("");
			record.setExtstr1("0");//已配置:1，未配置:0
			int result=expoBiz.updateByPrimaryKeySelective(record);
			
			if(result==1) {
	    		msg="设置成功！";
	    		return HttpResult.ok(msg);
	    	}else {
	    		msg="设置失败！";
	    		return HttpResult.error(msg);
			}
		}
	
	

}
